/**
 * Copyright 2006 Brad Reynolds
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.leeloo.databinding.internal.ui;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.debug.core.IJavaBreakpoint;
import org.eclipse.jdt.debug.core.IJavaLineBreakpoint;
import org.eclipse.jdt.debug.core.JDIDebugModel;

/**
 * Creates breakpoints.
 * 
 * @author Brad Reynolds
 */
public class BreakpointsBuilder {
    private static final IJavaLineBreakpoint[] NO_BREAKPOINTS = new IJavaLineBreakpoint[0];
    private List breakpoints;

    /**
     * Adds a line breakpoint.
     * 
     * @param resource
     * @param className
     * @param lineNumber
     * @param snippet
     */
    public void addLineBreakpoint(IResource resource, String className, int lineNumber, String snippet) {
        if (resource == null) {
            throw new IllegalArgumentException("Parameter resource was null."); //$NON-NLS-1$
        }
        if (className == null) {
            throw new IllegalArgumentException("Parameter className was null."); //$NON-NLS-1$
        }
        
        if (breakpoints == null) {
            breakpoints = new LinkedList();
        }

        breakpoints.add(new Breakpoint(resource, className, lineNumber, snippet));
    }

    /**
     * Creates the breakpoints.
     * 
     * @return created breakpoints
     */
    public IJavaBreakpoint[] build() {
        List results = null;

        if (breakpoints != null) {
            for (Iterator it = breakpoints.iterator(); it.hasNext();) {
                if (results == null) {
                    results = new LinkedList();
                }

                Breakpoint breakpoint = (Breakpoint) it.next();

                try {
                    IJavaLineBreakpoint lineBreakpoint = JDIDebugModel.createLineBreakpoint(breakpoint.resource,
                            breakpoint.className,
                            breakpoint.lineNumber,
                            -1,
                            -1,
                            -1,
                            true,
                            null);
                    
                    lineBreakpoint.setCondition(breakpoint.snippet);
                    lineBreakpoint.setConditionEnabled(true);
                    results.add(lineBreakpoint);
                } catch (CoreException e) {
                    throw new RuntimeException(e);
                }
            }

            breakpoints = null;
        }

        return (IJavaBreakpoint[]) ((results != null) ? results.toArray(new IJavaBreakpoint[results.size()]) : NO_BREAKPOINTS);
    }
    
    private static class Breakpoint {
        final IResource resource;
        final int lineNumber;
        final String snippet;
        final String className;

        Breakpoint(IResource resource, String className, int lineNumber, String snippet) {
            this.resource = resource;
            this.lineNumber = lineNumber;
            this.snippet = snippet;
            this.className = className;
        }
    }
}